#include "OLInfoButton.h"

#include <QPainter>
#include <QNetworkConfigurationManager>
#include <QNetworkConfiguration>
#include <QLabel>
#include <QHBoxLayout>
#include <QTime>

#include <OLAppTemplateDefs.h>
#include <OLPixmapWidget.h>

OLInfoButton::OLInfoButton(QWidget *parent) :
    QWidget(parent)
{
    m_batteryIcon = new QLabel(this);
    m_networkIcon = new QLabel(this);
    m_isRoaming = new OLPixmapWidget(":/infopanel/roaming");
    m_isRoaming->setOpacity(0.25);
    m_connectedGsm = new OLPixmapWidget(":/infopanel/3g");
    m_connectedGsm->setOpacity(0.25);
    m_connectedWifi = new OLPixmapWidget(":/infopanel/wifi");
    m_connectedWifi->setOpacity(0.25);

    QSystemDeviceInfo* devInfo = new QSystemDeviceInfo(this);
    connect(devInfo, SIGNAL(powerStateChanged(QSystemDeviceInfo::PowerState)), SLOT(handlePowerState(QSystemDeviceInfo::PowerState)));
    handlePowerState(devInfo->currentPowerState());

    connect(devInfo, SIGNAL(batteryLevelChanged(int)), SLOT(handleBatteryLevel(int)));
    handleBatteryLevel(devInfo->batteryLevel());

    m_netInfo = new QSystemNetworkInfo(this);
    connect(m_netInfo, SIGNAL(networkModeChanged(QSystemNetworkInfo::NetworkMode)), SLOT(handleNetworkMode(QSystemNetworkInfo::NetworkMode)));
    handleNetworkMode(m_netInfo->currentMode());

    m_netMan = new QNetworkConfigurationManager(this);
    connect(m_netMan, SIGNAL(configurationChanged(QNetworkConfiguration)), SLOT(handleIapChange(QNetworkConfiguration)));

    QSystemNetworkInfo::NetworkMode currentMode = m_netInfo->currentMode();
    QLabel* networkName = new QLabel(m_netInfo->networkName(currentMode));
    if(networkName->text().isEmpty()) {
        networkName->setText(tr("No Network"));
    }
    QFont font = networkName->font();
    font.setPixelSize(FONT_SIZE-4);
    networkName->setFont(font);

    m_time = new QLabel(QTime::currentTime().toString("hh:mm"), this);
    m_time->setFont(font);

    QHBoxLayout* layout = new QHBoxLayout(this);
    layout->setContentsMargins(5,0,5,0);
    layout->setSpacing(5);

    layout->addWidget(m_batteryIcon);
    layout->addWidget(m_networkIcon);
    layout->addWidget(networkName);
    layout->addStretch();
    layout->addWidget(m_isRoaming);
    layout->addWidget(m_connectedGsm);
    layout->addWidget(m_connectedWifi);
    layout->addWidget(m_time);

    setLayout(layout);

    // start the timer for updating the time
    startTimer(60000);
}

void OLInfoButton::handlePowerState(QSystemDeviceInfo::PowerState state)
{
    switch(state) {
    case QSystemDeviceInfo::BatteryPower: {
        m_isBattery = true;
        int iconLevel = qBound(0, (m_batteryLevel/20)+1, 5);
        QString iconPath = ":/infopanel/battery" + QString::number(iconLevel);
        m_batteryIcon->setPixmap(QPixmap(iconPath));
    } break;
    default:
        m_isBattery = false;
        m_batteryIcon->setPixmap(QPixmap(":/infopanel/battery0"));
    }

    update();
}

void OLInfoButton::handleBatteryLevel(int level)
{
    m_batteryLevel = level;

    if(m_isBattery)
        handlePowerState(QSystemDeviceInfo::BatteryPower);
}

void OLInfoButton::handleNetworkMode(QSystemNetworkInfo::NetworkMode mode)
{
    m_networkIcon->setPixmap(QPixmap(":/infopanel/network0"));
    disconnect(m_netInfo, SIGNAL(networkSignalStrengthChanged(QSystemNetworkInfo::NetworkMode,int)));
    disconnect(m_netInfo, SIGNAL(networkStatusChanged(QSystemNetworkInfo::NetworkMode,QSystemNetworkInfo::NetworkStatus)));

    switch(mode) {
    case QSystemNetworkInfo::GsmMode:
    case QSystemNetworkInfo::CdmaMode:
    case QSystemNetworkInfo::WcdmaMode: {
        connect(m_netInfo, SIGNAL(networkSignalStrengthChanged(QSystemNetworkInfo::NetworkMode,int)), SLOT(handleSignalStrength(QSystemNetworkInfo::NetworkMode,int)));
        handleSignalStrength(mode, m_netInfo->networkSignalStrength(mode));

        connect(m_netInfo, SIGNAL(networkStatusChanged(QSystemNetworkInfo::NetworkMode,QSystemNetworkInfo::NetworkStatus)), SLOT(handleNetworkStatus(QSystemNetworkInfo::NetworkMode,QSystemNetworkInfo::NetworkStatus)));
        handleNetworkStatus(mode, m_netInfo->networkStatus(mode));
    } break;
    default:
        break;
    }

    update();
}

void OLInfoButton::handleNetworkStatus(QSystemNetworkInfo::NetworkMode mode, QSystemNetworkInfo::NetworkStatus status)
{
    bool handleStatus = false;
    switch(mode) {
    case QSystemNetworkInfo::GsmMode:
    case QSystemNetworkInfo::CdmaMode:
    case QSystemNetworkInfo::WcdmaMode: {
        handleStatus = true;
    } break;
    default:
        break;
    }

    if(handleStatus) {
        switch(status) {
        case QSystemNetworkInfo::HomeNetwork: {
            m_isRoaming->setOpacity(0.25);
        } break;
        case QSystemNetworkInfo::Roaming: {
            m_isRoaming->setOpacity(1.0);
        } break;
        case QSystemNetworkInfo::Connected: {
        } break;
        default:
            m_networkIcon->setPixmap(QPixmap(":/infopanel/network0"));
            break;
        }
    }

    update();
}

void OLInfoButton::handleSignalStrength(QSystemNetworkInfo::NetworkMode mode, int strength)
{
    bool displaySignal = false;
    switch(mode) {
    case QSystemNetworkInfo::GsmMode:
    case QSystemNetworkInfo::CdmaMode:
    case QSystemNetworkInfo::WcdmaMode: {
        displaySignal = true;
    } break;
    default:
        break;
    }

    if(displaySignal) {
        if(strength>0) {
            int iconLevel = qBound(0, (strength/20)+1, 5);
            QString iconPath = ":/infopanel/network" + QString::number(iconLevel);
            m_networkIcon->setPixmap(QPixmap(iconPath));
        } else {
            // zer strength
            QString iconPath = ":/infopanel/network0";
            m_networkIcon->setPixmap(QPixmap(iconPath));
        }
    }

    update();
}

void OLInfoButton::handleIapChange(const QNetworkConfiguration& /*config*/)
{
    m_connectedGsm->setOpacity(0.25);
    m_connectedWifi->setOpacity(0.25);
    QList<QNetworkConfiguration> connectedConfigurations = m_netMan->allConfigurations(QNetworkConfiguration::Active);
    foreach(QNetworkConfiguration config, connectedConfigurations) {
        QNetworkConfiguration::BearerType bearer = config.bearerType();
        switch(bearer) {
        case QNetworkConfiguration::Bearer2G:
        case QNetworkConfiguration::BearerCDMA2000:
        case QNetworkConfiguration::BearerWCDMA:
        case QNetworkConfiguration::BearerHSPA: {
            m_connectedGsm->setOpacity(1.0);
        } break;
        case QNetworkConfiguration::BearerWLAN: {
            m_connectedWifi->setOpacity(1.0);
        } break;
        default:
            break;
        }
    }

    update();
}

void OLInfoButton::timerEvent(QTimerEvent *event)
{
    m_time->setText(QTime::currentTime().toString("hh:mm"));
}

//void OLInfoButton::paintEvent(QPaintEvent *e)
//{
//    OLButton::paintEvent(e);

//    QPainter p(this);

//    QPixmap threegIcon(":/infopanel/3g");
//    QPixmap wifiIcon(":/infopanel/wifi");
//    QPixmap roamingIcon(":/infopanel/roaming");

//    p.drawPixmap(5, 13, m_networkIcon);
//    p.drawPixmap(25, 13, m_batteryIcon);

//    int spacing = (threegIcon.width() + wifiIcon.width() + roamingIcon.width());
//    spacing = (width()-spacing)/4;

//    qreal opacity = m_connectedGsm ? 1.0 : 0.25;
//    p.setOpacity(opacity);
//    p.drawPixmap(spacing, 2, threegIcon);

//    opacity = m_connectedWifi ? 1.0 : 0.25;
//    p.setOpacity(opacity);
//    p.drawPixmap(spacing*2+threegIcon.width(), 2, wifiIcon);

//    opacity = m_isRoaming ? 1.0 : 0.25;
//    p.setOpacity(opacity);
//    p.drawPixmap(spacing*3+threegIcon.width()+wifiIcon.width(), 2, roamingIcon);
//}

